<?php

if (!class_exists('Database'))
{
	require_once 'bin/medoo.min.php';
	require_once 'wordpress-config.php';
	class Database {
			
			//Database documentation - http://medoo.in/doc
			private static $database = NULL;
			
			static function getDatabase()
			{
				if(self::$database == NULL)
					self::startDatabase();
				return self::$database;
			}
			
			function __constructor()
			{
				
			}
			
			private static function startDatabase()
			{
				self::$database = new medoo(array(
					// required
					'database_type' => 'mysql',
					'database_name' => CONF::$WPDatabaseName,
					'server' => CONF::$WPDatabaseServerAddress,
					'username' => 'root',
					'password' => '',
				 
					// optional
					'port' => 3306,
					'charset' => 'utf8',
					// driver_option for connection, read more from http://www.php.net/manual/en/pdo.setattribute.php
					'option' => array(
						PDO::ATTR_CASE => PDO::CASE_NATURAL
					)
				));
			}
			
			static function Select($tableName, $selectStatement, $where = NULL)
			{
				if(self::$database == NULL)
					self::startDatabase();
				$data = self::$database->select($tableName, $selectStatement, $where);
				return $data;
			}
			
			//inputArray or updateArray format
			//[
				//"type" => "user",
	 
				// All age plus one
				//"age[+]" => 1,
	 
				// All level subtract 5
				//"level[-]" => 5,
	 
				//"lang" => ["en", "fr", "jp", "cn", "de"]
			//], ONLY FOR UPDATE ARRAY when you need a WHERE statement 
			//[
				//"user_id[<]" => 1000
			//]
			
			static function Insert($tableName, $inputArray)
			{
				if(self::$database == NULL)
					self::startDatabase();
				$data = self::$database->insert($tableName, $inputArray);
				return $data;
			}
			
			static function Update($tableName, $updateArray, $whereArray = NULL)
			{
				if(self::$database == NULL)
					self::startDatabase();
				$data = self::$database->update($tableName, $updateArray, $whereArray);
				return $data;
			}
			
			static function Rollback($tableName)
			{
				if(self::$database == NULL)
					self::startDatabase();
				$data = self::$database->query("Rollback;");
			}
			
	}
}
?>